package cn.fxyz.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import cn.fxyz.dao.DinnertableDao;
import cn.fxyz.entity.Dinnertable;
import cn.fxyz.page.PageBean;
import cn.fxyz.service.DinnertableService;

public class DinnertableServiceImpl implements DinnertableService {
	private DinnertableDao dinnertableDao;
	public DinnertableDao getDinnertableDao() {
		return dinnertableDao;
	}
	public void setDinnertableDao(DinnertableDao dinnertableDao) {
		this.dinnertableDao = dinnertableDao;
	}
	public List<Dinnertable> findAll() {
		
		return dinnertableDao.selectAll();
		
	}
	//查询所有餐桌以及条件查询
	public List<Dinnertable> findAll(Dinnertable dinnertable) {
		StringBuffer stringBuffer = new StringBuffer();
		List<Object> paramList = new ArrayList<Object>();
		if(StringUtils.isNotBlank(dinnertable.getTablename())) {
			stringBuffer.append(" and tablename like ?");
			paramList.add("%" + dinnertable.getTablename() + "%");
		}
		Object[] params = paramList.toArray();
		String hql = stringBuffer.toString();
		return dinnertableDao.selectAll(hql, params);
		
	}
	//查询所有餐桌 + 条件查询 + 分页查询
	public PageBean<Dinnertable> findAll(Dinnertable dinnertable, int pageNum,
			int pageSize) {
		StringBuffer buffer = new StringBuffer();
		List<Object> paramList = new ArrayList<Object>();
		//条件查询
		if(StringUtils.isNotBlank(dinnertable.getTablename())) {
			buffer.append(" and tablename like ?");
			paramList.add("%" + dinnertable.getTablename() + "%");
		}
		String hql = buffer.toString();
		Object[] params = paramList.toArray();
		//分页
		//总记录数
		int totalRecord = dinnertableDao.getTotalRecord(hql,params);
		//创建对象
		PageBean<Dinnertable> pageBean = new PageBean<Dinnertable>(pageNum, pageSize, totalRecord);
		//分页数据
		List<Dinnertable> data = dinnertableDao.selectAll(hql, params,pageBean.getStartIndex(),pageBean.getPageSize());
		pageBean.setData(data);
		return pageBean;
	}
	//添加餐桌
	public void add(Dinnertable dinnertable) {
		dinnertable.setTablestatus(0);
		dinnertableDao.insert(dinnertable);
	}
	//通过id删除餐桌
	public void remove(Dinnertable dinnertable) {
		dinnertableDao.delete(dinnertable);
	}
	//退桌和预定
	public void modify(Dinnertable dinnertable) {
		Dinnertable selectById = dinnertableDao.selectById(dinnertable.getId());
		System.out.println(selectById.getTablestatus());
		if(dinnertable.getTablestatus() == 0) {
			Date date = new Date();
			selectById.setTablestatus(1);
			selectById.setOrderdate(date);
		}
		if(dinnertable.getTablestatus() == 1) {
			selectById.setTablestatus(0);
			selectById.setOrderdate(null);
		}
		
	}
	public List<Dinnertable> findByTableStatus() {
		return dinnertableDao.selectByTableStatus();
	}
	
	

}
